// component/ui/z-track/z-track.js
const path = '../z-tracks/z-tracks'
Component({
  relations: {
    [path]: {
      type: 'ancestor',
      linked: function (target) {},
      linkChanged: function (target) {},
      unlinked: function (target) {}
    }
  },

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/,
  },

  /**
   * 组件的属性列表
   */
  properties: {
    isHideLine: {
      type: Boolean,
      value: false,
    },
    mainTitle: {
      type: String,
      value: '',
    },
    subTitle: {
      type: String,
      value: '',
    },
    name: {
      type: String,
      value: '',
    },
    value: {
      type: Boolean,
      value: false,
    },
    color: {
      type: String,
      value: '#d8d8d8',
    },
    activeColor: {
      type: String,
      value: '#d8d8d8',
    },
  },

  /**
   * 组件的数据监听
   */
  observers: {
    value(val) {
      this.setData({
        isActive: val
      })
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    isActive: false,
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    created() {
      Object.defineProperty(this, 'parent', {
        get: () => this.getRelationNodes(path)[0],
      });
    },
    attached() {},
    detached() {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateActive() {
      if (!this.parent) {
        return;
      }
      const {
        current,
      } = this.parent.data;
      const {
        children = []
      } = this.parent;
      const {
        name
      } = this.data;
      const index = children.indexOf(this);
      const currentName = String(name ? name : index);
      const isActive = current === currentName;
      this.setData({
        isActive
      });
    },
  }

})